/*

* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Smart-Shop开源商城系统/ Java商城-首选电商平台系统 请务必保留此注释头信息
* |     开源地址: https://gitee.com/ningbo-qishan/gruul-mall
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     可正常分享和学习源码,未经授权 不可商用！
* |     商业版中台购买/开源版商业授权 联系技术客服
* |     官网:  https://www.73app.cn/
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Copyright (c) 2016-2999 宁波启山智软科技有限公司
* |     版权所有 ,侵权必究！
* ------------------------------------------------------------------------

*/
package com.medusa.gruul.payment.handler.refundNotify;

import cn.hutool.core.util.StrUtil;
import com.egzosn.pay.common.bean.PayMessage;
import com.medusa.gruul.common.core.config.Jackson;
import com.medusa.gruul.common.core.exception.ServiceException;
import com.medusa.gruul.common.core.handler.Handler;
import com.medusa.gruul.payment.api.entity.PaymentRefund;
import com.medusa.gruul.payment.api.enums.PaymentQueueEnum;
import com.medusa.gruul.payment.api.model.dto.RefundNotifyResultDto;
import com.medusa.gruul.payment.common.constant.AliConst;
import com.medusa.gruul.payment.service.IPaymentRefundService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;

import java.util.Map;

/**
 * @author 张治保
 * date 2022/1/25
 */
@Slf4j
@RequiredArgsConstructor
public abstract class AbstractRefundNotifyHandler implements Handler<Void> {

    private final IPaymentRefundService paymentRefundService;
    private final AmqpTemplate amqpTemplate;

    @Override
    public Void handle(Object... params) {
        if (hasErrorParam(params,PayMessage.class)){
            throw new ServiceException("参数错误");
        }
        PayMessage message = cast(params[0], PayMessage.class);
        String refundNo = refundNo(message.getPayMessage());
        log.info("::退款回调::RefundNo::{}",refundNo);
        if(StrUtil.isEmpty(refundNo)){
            throw new ServiceException("未正确获取退款单号");
        }
        PaymentRefund refund = paymentRefundService.lambdaQuery()
            .eq(PaymentRefund::getOrderId, refundNo)
            .one();
        if(refund == null){
            return null;
        }
        if (StrUtil.isNotEmpty(refund.getSynCallback())){
            return null;
        }
        String outTradeNo = message.getOutTradeNo();
        /*
         * 通知退款消息
         */
        amqpTemplate.convertAndSend(
            PaymentQueueEnum.QUEUE_PAYMENT_NOTIFY_SUCCESS.getExchange(),
            refund.getRouteKey(),
            refundNotifyResult(refund.getTenantId(), outTradeNo,refundNo)
        );
        /*
         * 更新退款异步结果
         */
        refund.setSynCallback(Jackson.writeValueAsString(message));
        paymentRefundService.updateById(refund);
        log.info("::::退款回调接口执行完毕::OutTradeNo:{}::RefundNo:{}",outTradeNo,refundNo);
        return null;
    }

    /**
     * 获取MQ通知消息
     * @return mq通知消息
     */
    protected RefundNotifyResultDto refundNotifyResult(String tenantId,String outTradeNo,String refundNo){
        RefundNotifyResultDto result = new RefundNotifyResultDto();
        result.setTenantId(tenantId);
        result.setOutTradeNo(outTradeNo);
        result.setRefundId(refundNo);
        return result;
    }

    /**
     * 获取退款单号
     */
    protected abstract String refundNo(Map<String,Object> message);
}
